<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Administrator controller
 */
class Admin extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Index page 
     */
    function index()
    {
        // Configure web page
        $this->data['title'] = 'Admin Home';
        $this->data['menu'] = 'home';
        $this->data['page'] = 'admin/index';
        $this->data['icon'] = array('file' => 'home.png', 'name' => 'beranda');

        $this->load->view('admin_view', $this->data);
    }

    /**
     *  User page
     */
    function user()
    {
        // Redirect to respective page/function
        $segment = $this->uri->segment(3, null);
        if (!is_null($segment)) {
            switch ($segment) {
                case 'add': redirect('auth/create_user', 'refresh');
                    return;
                case 'edit': $this->user_edit($this->uri->segment(4, null));
                    return;
                case 'delete': $this->user_delete($this->input->post('id'));
                    return;
            }
        }

        // Get user data
        $this->data['users'] = $this->ion_auth->get_users('members');

        // Configure web page
        $this->data['title'] = 'Web Admin';
        $this->data['page'] = 'admin/user';
        $this->data['menu'] = 'admin';
        $this->data['icon'] = array('file' => 'admin.png', 'name' => 'admin');
        $this->data['css'] = array('jquery.tablesorter.css' => '');
        $this->data['js'] = array('jquery.tablesorter.js', 'jquery.tablesorter.pager.js', 'web.admin.user.js');

        $this->load->view('admin_view', $this->data);
    }

    /**
     * Edit user
     * @param int $id user ID 
     */
    private function user_edit($id)
    {
        // Get post parameters
        $post = $this->input->post();
        if (!$post) { //open from user list
            $user = $this->ion_auth->get_user($id);
            $this->data['form_data'] = array(
                'first_name' => $user->first_name,
                'last_name' => $user->last_name,
                'email' => $user->email,
                'contact' => $user->contact
            );
        } else { //open from submit
            //validate form input
            $this->form_validation->set_rules('first_name', 'Nama Depan', 'required|xss_clean');
            $this->form_validation->set_rules('last_name', 'Nama Belakang', 'required|xss_clean');
            $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
            $this->form_validation->set_rules('contact', 'Telp/Ponsel', 'required|xss_clean');

            if ($this->form_validation->run() == true) {
                $new_data = array(
                    "username" => $this->input->post('email'),
                    "email" => $this->input->post('email'),
                    "first_name" => $this->input->post('first_name'),
                    "last_name" => $this->input->post('last_name'),
                    "contact" => $this->input->post('contact')
                );

                if ($this->ion_auth->update_user($id, $new_data)) {
                    $this->session->set_flashdata('message', 'Admin telah ter-update');
                } else {
                    $this->session->set_flashdata('message', "<span class='error'>Admin gagal di-update</span>");
                }
                
                redirect('admin/user', 'redirect');
            } else {
                $this->data['form_data'] = array(
                    'first_name' => $this->form_validation->set_value('first_name'),
                    'last_name' => $this->form_validation->set_value('last_name'),
                    'email' => $this->form_validation->set_value('email'),
                    'contact' => $this->form_validation->set_value('contact')
                );
            }
        }

        // Configure web page
        $this->data['title'] = "Edit Admin";
        $this->data['page'] = 'admin/user_edit';
        $this->data['menu'] = 'admin';
        $this->data['icon'] = array('file' => 'admin.png', 'name' => 'admin');

        //display the create user form
        //set the flash data error message if there is one
        $this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

        $this->load->view('admin_view', $this->data);
    }

    /**
     * Delete user
     * @param int $id user ID 
     */
    private function user_delete($id)
    {
        if ($this->ion_auth->delete_user($id)) {
            $this->session->set_flashdata('message', 'Admin telah terhapus');
        } else {
            $this->session->set_flashdata('message', $this->ion_auth->errors());
        }
        
        redirect('admin/user', 'refresh');
    }

    /**
     * Display articles 
     */
    function article()
    {
        // Redirect to respective page/function
        $segment = $this->uri->segment(3, null);
        if (!is_null($segment)) {
            switch ($segment) {
                case 'add': $this->article_add($this->uri->segment(4, null));
                    return;
                case 'edit': $this->article_edit($this->uri->segment(4, null));
                    return;
                case 'delete': $this->article_delete($this->input->post('id'));
                    return;
            }
        }

        // Get user data
        $this->load->model('artikel_model');
        $this->data['articles'] = $this->artikel_model->getAllArtikel();

        $this->data['title'] = 'Artikel';
        $this->data['menu'] = 'article';
        $this->data['page'] = 'admin/article';
        $this->data['icon'] = array('file' => 'article.png', 'name' => 'artikel');
        $this->data['css'] = array('jquery.tablesorter.css' => '');
        $this->data['js'] = array('jquery.tablesorter.js', 'jquery.tablesorter.pager.js', 'web.admin.article.js');

        $this->load->view('admin_view', $this->data);
    }

    /**
     * Insert new article
     */
    private function article_add()
    {
        $post = $this->input->post();
        if ($post) {
            //validate form input
            $this->form_validation->set_rules('title', 'Judul', 'required|xss_clean');
            $this->form_validation->set_rules('content', 'Isi Artikel', 'required|xss_clean');

            // When valid
            if ($this->form_validation->run()) {
                // Set insert data here
                $data = array(
                    'artikel_name' => $post['title'],
                    'artikel_content' => $post['content']
                );

                $this->load->model('artikel_model');
                if ($this->artikel_model->insertArtikel($data)) {
                    $this->session->set_flashdata('message', 'Artikel baru telah dimasukkan');
                } else {
                    $this->session->set_flashdata('message', "<span class='error'>Artikel baru gagal dimasukkan</span>");
                }
                
                redirect('admin/article');
            }
        }

        //set the flash data error message if there is one
        $this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

        // Form data
        $this->data['form_data'] = array(
            'title' => $this->form_validation->set_value('title'),
            'content' => $this->form_validation->set_value('content')
        );

        // Configure web page
        $this->data['title'] = "Tambah Artikel";
        $this->data['page'] = 'admin/article_add';
        $this->data['menu'] = 'article';
        $this->data['icon'] = array('file' => 'article.png', 'name' => 'artikel');
        $this->data['js'] = array('tiny_mce/tiny_mce.js','web.admin.article.editor.js');

        $this->load->view('admin_view', $this->data);
    }
    
    private function article_edit($id){
        $this->load->model('artikel_model');
        
        // Get post parameters
        $post = $this->input->post();
        if (!$post) { //open from user list
            $article = $this->artikel_model->getDetailArtikel($id);
            $this->data['form_data'] = array(
                'title' => $article->artikel_name,
                'content' => $article->artikel_content
            );
        } else { //open from submit
            //validate form input
            $this->form_validation->set_rules('title', 'Judul', 'required|xss_clean');
            $this->form_validation->set_rules('content', 'Isi Artikel', 'required|xss_clean');

            if ($this->form_validation->run() == true) {
                $new_data = array(
                    "artikel_name" => $post['title'],
                    "artikel_content" => $post['content']
                );

                if ($this->artikel_model->updateArtikel($id, $new_data)) {
                    $this->session->set_flashdata('message', 'Artikel telah ter-update');
                } else {
                    $this->session->set_flashdata('message', "<span class='error'>Artikel gagal di-update</span>");
                }
                
                redirect('admin/article', 'redirect');
            } else {
                $this->data['form_data'] = array(
                    'title' => $this->form_validation->set_value('title'),
                    'content' => $this->form_validation->set_value('content')
                );
            }
        }

        // Configure web page
        $this->data['title'] = "Edit Artikel";
        $this->data['page'] = 'admin/article_edit';
        $this->data['menu'] = 'article';
        $this->data['icon'] = array('file' => 'article.png', 'name' => 'artikel');
        $this->data['js'] = array('tiny_mce/tiny_mce.js','web.admin.article.editor.js');

        //display the create user form
        //set the flash data error message if there is one
        $this->data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));

        $this->load->view('admin_view', $this->data);
    }

    private function article_delete($id){
        $this->load->model('artikel_model');
        
        if ($this->artikel_model->deleteArtikel($id)) {
            $this->session->set_flashdata('message', 'Artikel telah terhapus');
        } else {
            $this->session->set_flashdata('message', "<span class='error'>Artikel gagal dihapus</span>");
        }
        
        redirect('admin/article', 'refresh');
    }
}
?>
